package org.example.utils;

import org.example.config.JdbcConfig;
import org.example.pojo.Student;

import java.sql.*;
import java.util.ArrayList;

public class DBUtils {
    private static final String driver;
    private static final String url;
    private static final String username;
    private static final String password;
    private static final Connection connection;
    private static final Statement statement;
    private static PreparedStatement preparedStatement;

    static {
        driver=JdbcConfig.getDriver();
        url = JdbcConfig.getUrl();
        username=JdbcConfig.getUsername();
        password=JdbcConfig.getPassword();
        try {
            connection = DriverManager.getConnection(url,username,password);
            statement = connection.createStatement();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //抽象出的获取PreparedStatement
    private static void getPreparedStatement(String sql){
        try {
            preparedStatement = connection.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //执行增删改的方法
    public static int Save(String sql, Object[] params){
        try {
            getPreparedStatement(sql);
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(i+1,params[i]);
            }
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static ArrayList<Student> getAll(String sql){
        try {
            ResultSet resultSet = statement.executeQuery(sql);
            ArrayList<Student> list = new ArrayList<>();
            while(resultSet.next()){
                Student student = new Student();
                student.setId(resultSet.getInt("id"));
                student.setStuName(resultSet.getString("stu_name"));
                student.setStuGender(resultSet.getString("stu_gender"));
                student.setStuBrithday(resultSet.getDate("stu_birthday"));
                student.setStuUsername(resultSet.getString("stu_username"));
                student.setStuPassword(resultSet.getString("stu_password"));
                list.add(student);
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static int DeleteById(String sql,Integer id){
        try {
            getPreparedStatement(sql);
            preparedStatement.setInt(1,id);
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static Student getById(String sql,Integer id){
        Student student = new Student();
        try {
            getPreparedStatement(sql);
            preparedStatement.setInt(1,id);
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                student.setId(resultSet.getInt("id"));
                student.setStuBrithday(resultSet.getDate("stu_birthday"));
                student.setStuGender(resultSet.getString("stu_gender"));
                student.setStuName(resultSet.getString("stu_name"));
                student.setStuPassword(resultSet.getString("stu_password"));
                student.setStuUsername(resultSet.getString("stu_username"));
            }
            return student;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static int Update(String sql,Object[] params){
        try {
            getPreparedStatement(sql);
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(i+1,params[i]);
            }
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void close(){
        try {
            if(statement!=null){
                statement.close();
            }
            if(connection!=null){
                connection.close();
            }
            if (preparedStatement!=null){
                preparedStatement.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
